package com.geeke.wf.candidate;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.camunda.bpm.engine.delegate.VariableScope;

import com.alibaba.fastjson.JSONObject;
import com.geeke.camundaex.candidate.ICandidate;
import com.geeke.camundaex.entity.ActUser;
import com.geeke.common.data.PageRequest;
import com.geeke.common.data.Parameter;
import com.geeke.sys.dao.UserPostDao;
import com.geeke.sys.entity.Post;
import com.geeke.sys.entity.UserPost;
import com.geeke.utils.SpringUtils;
import com.google.common.collect.Lists;

/**
 * 获取岗位对应用户作为审批后续人
 * @author 
 *
 */
public class CandidateByPost extends SpringUtils implements ICandidate {
	@Override
	public List<ActUser> execute(VariableScope variableScope, String strPosts) {
		List<ActUser> actUsers = Lists.newArrayList();
		if(StringUtils.isBlank(strPosts)) {
			return actUsers;
		}

		List<Post> posts = JSONObject.parseArray(strPosts, Post.class);
		if(posts == null || posts.size() == 0) {
			return actUsers;
		}

		UserPostDao userPostDao = (UserPostDao) getBean("userPostDao");
		
		List<String> postIds = Lists.newArrayList();
		for(Post post: posts) {
			postIds.add(post.getId());
		}

		List<Parameter> params = Lists.newArrayList();
		
		PageRequest pageRequest;
		/*获取子表列表   用户岗位*/
		params.add(new Parameter("post_id", "in", postIds));
		pageRequest = new PageRequest(params);

		List<UserPost> users = userPostDao.listAll(pageRequest);
		if(users != null) {
			for(UserPost u: users) {
				actUsers.add(new ActUser(u.getUser().getId(), u.getUser().getName()));
			}
		}
		
		return actUsers;
	}
}
